package com.evildog.workspace.life.application.controller;

import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.life.application.bo.NoteBookBO;
import com.evildog.workspace.life.application.bo.NoteBookPageBO;
import com.evildog.workspace.life.application.convert.NoteBookConvert;
import com.evildog.workspace.life.application.model.NoteBookPageParam;
import com.evildog.workspace.life.application.model.NoteBookParam;
import com.evildog.workspace.life.application.service.NoteBookService;
import com.evildog.workspace.life.application.vo.NoteBookPageVO;
import com.evildog.workspace.life.application.vo.NoteBookVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * noteBook Controller
    笔记本
 * </p>
 *
 * @author evildog
 * @since 2020-08-04
 */
@RestController
@RequestMapping("/note-books")
@AllArgsConstructor
@Api(tags = "noteBook相关接口")
public class NoteBookController {

    private NoteBookService noteBookService;

    @PostMapping
    @ApiOperation(value = "添加接口")
    public Result<Boolean> save(@RequestBody NoteBookParam noteBookParam) {
        return ResultGenerator.status(noteBookService.save(NoteBookConvert.INSTANCE.convert(noteBookParam)));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultGenerator.status(noteBookService.removeById(id));
    }

    @DeleteMapping("/batch-remove")
    @ApiOperation(value = "批量删除接口")
    public Result<Boolean> removeByIds(@RequestParam("ids") List<Integer> ids) {
        return ResultGenerator.status(noteBookService.removeByIds(ids));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新接口")
    public Result<Boolean> updateById(@PathVariable("id") Long id,@RequestBody NoteBookParam noteBookParam) {
        return ResultGenerator.status(noteBookService.updateById(id, NoteBookConvert.INSTANCE.convert(noteBookParam)));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询详情接口")
    public Result<NoteBookVO> getById(@PathVariable("id") Long id) {
        NoteBookBO noteBookBO = noteBookService.getDetailById(id);
        return ResultGenerator.success(NoteBookConvert.INSTANCE.convert(noteBookBO));
    }

    @GetMapping("/page")
    @ApiOperation(value = "表分页查询接口")
    public Result<PagedResult<NoteBookPageVO>> getPageList(NoteBookPageParam noteBookPageParam, PageQuery query){
        PagedResult<NoteBookPageBO> noteBookPageBO = noteBookService.getPageList(
                        query,
                        QueryConditionUtils.getQueryCondition(noteBookPageParam),
                        NoteBookConvert.INSTANCE.convert(noteBookPageParam));
        return ResultGenerator.success(NoteBookConvert.INSTANCE.convert(noteBookPageBO));
    }
}